package com.example.servic;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;

public class SercActivity extends AppCompatActivity implements View.OnClickListener {
   Intent t;
    Mycont ct;
    MyService bind;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_serc);
        //Intent t=new Intent(this，MyService.class);
        //t=new Intent(this，MyService.clas)
        startService(t);





    }

    @Override
    public void onClick(View v) {

        switch (v.getId())
        {
            case R.id.b1:
                startService(t);
                break;

            case R.id.b2:

                break;

            //通过Ibinder服务在进行绑定服务时通过参数ServiceConnection接受一个binder对象
            case R.id.b3:
                //参数为（INTENT service,Serviceconnection,flags）
                bindService(t,ct,BIND_ABOVE_CLIENT);
                break;
         //应用终止服务不会终止
            case R.id.b4:
              unbindService(ct);
                break;

        }

    }

class Mycont implements ServiceConnection
    {


        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
             //通过bindle调用服务中的方法接受服务返回的binder
            //long end=System.currentTimeMillis()+20*1000;
             bind = (MyService) service;

        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    }


}
